<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="db.Tob"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="db.Purmat"%>
<%@page import="db.PurmatDAO"%>
<%@page import="db.Purreq"%>
<%@page import="db.Group" %>
<%@page import="db.Purmat" %>
<%@page import="db.TobDAO" %>
<%@page import="db.PlaceDAO" %>
<%@page import="db.Project" %>
<%@page import="db.Place" %>
<%@page import="db.UserDAO" %>
<%@page import="db.User"%>
<%@page import="db.Substore"%>
<%@page import="db.SubstoreDAO"%>
<%@page import="db.Fororder"%>
<%@page import="db.FororderDAO"%>
<%@page import="db.Material"%>
<%@page import="db.MaterialDAO"%>
<%@page import="db.PurmatDAO"%>
<%@page import="db.Tmppur"%>
<%@page import="db.TmppurDAO"%>
<%@page import="db.Supplier"%>
<%@page import="db.Forderpurmat"%>
<%@page import="db.Fororder"%>
<%@page import="db.MaterialDAO"%>
<%@page import="db.Material"%>
<%@page import="db.ForderpurmatDAO"%>
<%@ include file="/jsp/common/utility/volti.jsp" %>
<%@ include file="/jsp/common/utility/logined.jsp" %>
<%
	request.setCharacterEncoding("utf-8");
	WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
	SubstoreDAO substoreDAO = (SubstoreDAO)ctx.getBean("SubstoreDAO");
	TobDAO tobDAO = (TobDAO)ctx.getBean("TobDAO");
	FororderDAO fororderDAO = (FororderDAO)ctx.getBean("FororderDAO");
	MaterialDAO materialDAO = (MaterialDAO)ctx.getBean("MaterialDAO");
	PlaceDAO placeDAO = (PlaceDAO)ctx.getBean("PlaceDAO");
	TmppurDAO tmppurDAO = (TmppurDAO)ctx.getBean("TmppurDAO");
	PurmatDAO purmatDAO = (PurmatDAO)ctx.getBean("PurmatDAO");
	
	ForderpurmatDAO forderpurmatDAO = (ForderpurmatDAO)ctx.getBean("ForderpurmatDAO");
	
	String rtype = request.getParameter("receivetype");
	String rid = request.getParameter("receiveid");
	List orderlist = new ArrayList();
	Map map = new HashMap();
	int uid = 0 ;
	if(session.getAttribute("id")!=null){
		uid = (Integer)session.getAttribute("id");
	}
	
	if(rid!=null && rtype!=null && (rtype=="0" ||"0".equalsIgnoreCase(rtype))){//临时订单
		int prid = Integer.parseInt(rid);
		Tmppur tmppur = tmppurDAO.findById(prid);
		
		if(tmppur!=null){
			Purreq purreq= tmppur.getPurreq();
			Project project = purreq.getProject();
			Supplier supplier = purreq.getSupplier();
			map.put("orderCode",tmppur.getTmpCode());
			if(project!=null){
				map.put("procode",project.getCode());
			}
			if(supplier!=null){
				map.put("supplier",supplier.getName());//供应商
			}
			
			if(purreq!=null){
				map.put("orderremark",purreq.getRemark());
			}
			map.put("substoreid","");
			map.put("bustypeid",purreq.getTobId());//业务类型
			List pulist = purmatDAO.findByTmppurId(tmppur.getId());
			if(pulist!=null && pulist.size()>0){
				for(int pi=0;pi<pulist.size();pi++){
					Purmat purmat = (Purmat)pulist.get(pi);
					Material material =new Material();
					material =materialDAO.findById(purmat.getMaterialId());
					Map omap = new HashMap();
					omap.put("mId",purmat.getId());//增加物资id。再隐藏的控件里传给下一个jsp 
					omap.put("mcode",material.getCode());
					omap.put("mname",material.getName());
					omap.put("mcom",material.getCompany());
					omap.put("mbatch",tmppur.getTmpCode());
					omap.put("procode",project.getCode());
					//omap.put("ordernum",purmat.getBuyNum());
					System.out.println("=====================" + purmat.getWareNum());
					omap.put("ordernum",purmat.getWareNum());//直接读取入库数量。进行入库时显示。每次入库后进行数量增减。
					omap.put("orderprice",purmat.getPrice());
					orderlist.add(omap);
				}
			}
		}
	}
	
	
	
	
	if(rid!=null && rtype!=null && (rtype=="1" ||"1".equalsIgnoreCase(rtype))){//正式订单
		int prid = Integer.parseInt(rid);
		Fororder fororder = fororderDAO.findById(prid);
		
		
		if(fororder!=null){
			map.put("orderCode",fororder.getOrderCode());
			map.put("supplier",fororder.getSupplier());//供应商
			map.put("orderremark",fororder.getOrderRemark());
			map.put("substoreid",fororder.getSubstoreId());
			map.put("procode",fororder.getProCode());
			map.put("bustypeid",0);//业务类型
			//material = materialDAO.findById(fororder.get.getMaterialId()); 
		}
		List fpulist = forderpurmatDAO.findByForOrderId(prid);
		if(fpulist!=null && fpulist.size()>0){
				for(int pi=0;pi<fpulist.size();pi++){
					Forderpurmat forderpurmat = (Forderpurmat)fpulist.get(pi);
					Material material =new Material();
					material =materialDAO.findById(forderpurmat.getMaterialId());
					Map omap = new HashMap();
					omap.put("mcode",material.getCode());
					omap.put("mname",material.getName());
					omap.put("mcom",material.getCompany());
					omap.put("mbatch",fororder.getOrderCode());
					omap.put("procode",forderpurmat.getProCode());
					omap.put("ordernum",forderpurmat.getOrderNum());
					
					omap.put("orderprice",forderpurmat.getOrderPrice());
					orderlist.add(omap);
				}
			}
	}
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	SimpleDateFormat codesdf = new SimpleDateFormat("yyyyMMddHHmmss");
	String rdate = sdf.format(new Date());
	UserDAO userDAO = (UserDAO)ctx.getBean("UserDAO");
	User user = userDAO.findById(uid);
	Group group = user.getGroup();
	
	request.setAttribute("sublist",substoreDAO.findAll());
	request.setAttribute("place",placeDAO.findAll());
	request.setAttribute("orderlist",orderlist);
	request.setAttribute("ordermap",map);
	request.setAttribute("toblist",tobDAO.findAll());
	request.setAttribute("groupname",group.getName());
	request.setAttribute("reCode","J" + codesdf.format(new Date()));
	request.setAttribute("rdate",rdate);
	request.setAttribute("type",rtype);
	pageContext.forward("/vm/procure/procureReList.vm");
%>
<%!
	
%>